Program util_vol_turn_decile
      character*6 ticker
      character*8 cusp,cusip
      integer(kind=8):: nshrs,nvalue,jvalue,ivol,numshrs(5500),ntot(5500),navail(5500),iperm(5500)
      dimension nep(5500),ntype(5500),jyr(5500),jmn(5500),jdy(5500),vwret(5500),retrn(5500),pctonloan(5500),jdate(5500),tfact(5000),favg(5500),fret(5500)
      dimension pctavail(5500),ivol(5500),ishrout(5500),price(5500),turno(5500),aturn(10),aprc(10),nms(100000),ndate(100000,52),factr(100000,52)
      dimension aret(10),avgfee(10),util(5500),retsq(5500),aretsq(10),autil(10) 
!      open(unit=10,file='e:\FIS_duration\episode_data_type_total.txt')
!      open(unit=11,file='e:\FIS_duration\splits.prn')
!      open(unit=99,file='e:\FIS_duration\util_volt_turn_decile.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_type_total_rep.txt')
      open(unit=11,file='e:\FIS_duration\splits.prn')
      open(unit=99,file='e:\FIS_duration\util_volt_turn_decile_rep.txt')
      nms=0
      do 700 k=1,3587
      read(11,1100)kprm,kdate,fct
      write(6,1100)kprm,kdate,fct
1100  format(1x,i5,1x,i8,1x,f9.5)
      nms(kprm)=nms(kprm)+1
      ndate(kprm,nms(kprm))=kdate
      factr(kprm,nms(kprm))=fct
700   continue      
      
      
      iend=0
      i=0
1     i=i+1      
      read(10,1000,end=900)nep(i),ntype(i),jast,jyr(i),jmn(i),jdy(i),cusp,ticker,itickt,numshrs(i),age,favg(i),fmax,fmin,fstd,iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),navail(i),ntot(i)
1000  format(1x,i4,1x,i1,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12) 
      turno(i)=float(ivol(i))/(1000*float(ishrout(i)))
      jdate(i)=jyr(i)*10000+jmn(i)*100+jdy(i)
      retsq(i)=retrn(i)*retrn(i)
      if(retrn(i).lt.-1.) retsq(i)=-9.9
      if(retrn(i).lt.-1.) write(6,6002)nep(i),retrn(i)
6002  format(1x,i4,1x,f9.6)      
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.50) go to 100
      if(nms(iperm(1)).eq.0) go to 18
      do 16 nn=1,nms(iperm(1))
!          write(6,6001)nep(1),nn
!6001  format(1x,i4,1x,i2)          
          do 10 ii=1,icnt
              if(jdate(ii).ge.ndate(iperm(1),nn)) price(ii)=price(ii)*(1.+factr(iperm(1),nn))
10        continue
16    continue          
18    icntm=icnt-1
      do 120 i1=1,icntm
          fret(i1)=1.
          ip=i1+1
          do 110 i2=ip,icnt
110       fret(i1)=fret(i1)*(1.+retrn(i2))
          fret(i1)=fret(i1)-1.
120   continue
      do 20 i1=1,icntm
          ip=i1+1
          do 15 i2=ip,icnt
              if(turno(i1).le.turno(i2)) go to 15
              tmp1=turno(i1)
              tmp2=price(i1)
              tmp3=favg(i1)
              tmp4=fret(i1)
              tmp5=util(i1)
              tmp6=retsq(i1)
              turno(i1)=turno(i2)
              price(i1)=price(i2)
              favg(i1)=favg(i2)
              fret(i1)=fret(i2)
              util(i1)=util(i2)
              retsq(i1)=retsq(i2)
              turno(i2)=tmp1
              price(i2)=tmp2
              favg(i2)=tmp3
              fret(i2)=tmp4
              util(i2)=tmp5
              retsq(i2)=tmp6
15        continue
20    continue
      tprice=0.
      do 30 ii=1,icnt
30    tprice=tprice+abs(price(ii))
      avgprc=tprice/float(icnt)
          rcnt=float(icnt)
          rcnt10=rcnt/10.
          icnt10=int(rcnt10)
          idif=icnt-10*icnt10
          aturn=0.
          aprc=0.
          avgfee=0.
          aret=0.
          aretsq=0.
          autil=0.
          do 40 n=1,4
              ibg=(n-1)*icnt10+1
              iend=n*icnt10
              cntutil=0.
              do 35 ii=ibg,iend
                  aturn(n)=aturn(n)+turno(ii)/(float(icnt10))
                  avgfee(n)=avgfee(n)+favg(ii)/(float(icnt10))
                  aprc(n)=aprc(n)+abs(price(ii))/(avgprc*float(icnt10))
                  aret(n)=aret(n)+fret(ii)/(float(icnt10))
                  aretsq(n)=aretsq(n)+retsq(ii)/(float(icnt10))
                  if(util(ii).ge.0.) cntutil=cntutil+1.
35            continue
              if(cntutil.lt.1.) autil(n)=-9.9
              if(cntutil.lt.1.) go to 40
              do 37 ii=ibg,iend
                  if(util(ii).lt.0.) go to 37
                  autil(n)=autil(n)+util(ii)/cntutil
37        continue           
40        continue
          ibg=4*icnt10+1
          iend=5*icnt10+idif
          cntutil=0.
          do 50 ii=ibg,iend
              aturn(5)=aturn(5)+turno(ii)/(float(icnt10)+float(idif))
              avgfee(5)=avgfee(5)+favg(ii)/(float(icnt10)+float(idif))
              aprc(5)=aprc(5)+abs(price(ii))/(avgprc*(float(icnt10)+float(idif)))
              aret(5)=aret(5)+fret(ii)/(float(icnt10)+float(idif))
              aretsq(5)=aretsq(5)+retsq(ii)/(float(icnt10)+float(idif))
              if(util(ii).ge.0.) cntutil=cntutil+1.
50        continue     
           do 52 ii=ibg,iend
           if(util(ii).lt.0.) go to 52
           autil(5)=autil(5)+util(ii)/cntutil
52         continue    
           if(cntutil.lt.1.) autil(5)=-9.9
          do 60 n=6,10
          cntutil=0.
              ibg=(n-1)*icnt10+1+idif
              iend=n*icnt10+idif
              do 55 ii=ibg,iend
                  aturn(n)=aturn(n)+turno(ii)/float(icnt10)
                  avgfee(n)=avgfee(n)+favg(ii)/(float(icnt10))
                  aprc(n)=aprc(n)+abs(price(ii))/(avgprc*float(icnt10))
                  aret(n)=aret(n)+fret(ii)/(float(icnt10))
                  aretsq(n)=aretsq(n)+retsq(ii)/(float(icnt10))
              if(util(ii).ge.0.) cntutil=cntutil+1.
55            continue
              if(cntutil.lt.1.) autil(n)=-9.9
              if(cntutil.lt.1.) go to 60
              do 57 ii=ibg,iend
                  if(util(ii).lt.0.) go to 57
                  autil(n)=autil(n)+util(ii)/cntutil
57        continue           
60        continue
!      write(6,9900)nep(1),ntype(1),icnt,(aturn(n),n=1,10),(aprc(n),n=1,10),(avgfee(n),n=1,10),(aret(n),n=1,10)
!      write(99,9900)nep(1),ntype(1),icnt,(aturn(n),n=1,10),(aprc(n),n=1,10),(avgfee(n),n=1,10),(aret(n),n=1,10)
!9900  format(1x,i4,1x,i1,1x,i4,10(1x,f9.6),2x,10(1x,f10.5),2x,10(1x,f10.4),2x,10(1x,f9.6))      
      write(6,9900)nep(1),ntype(1),icnt,(aturn(n),n=1,10),(aretsq(n),n=1,10),(autil(n),n=1,10),(aret(n),n=1,10)
      write(99,9900)nep(1),ntype(1),icnt,(aturn(n),n=1,10),(aretsq(n),n=1,10),(autil(n),n=1,10),(aret(n),n=1,10)
9900  format(1x,i4,1x,i1,1x,i4,10(1x,f9.6),2x,10(1x,f11.7),2x,10(1x,f6.2),2x,10(1x,f9.6))      
100   continue
      nep(1)=nep(i)
      ntype(1)=ntype(i)
      jyr(1)=jyr(i)
      jmn(1)=jmn(i)
      jdy(1)=jdy(i)
      retrn(1)=retrn(i)
      vwret(1)=vwret(i)
      ntot(1)=ntot(i)
      numshrs(1)=numshrs(i)
      navail(1)=navail(i)
      price(1)=price(i)
      turno(1)=turno(i)
      ivol(1)=ivol(i)
      ishrout(1)=ishrout(i)
      favg(1)=favg(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
    end program util_vol_turn_decile